﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV.Database;
using System.Linq;

namespace QLTV.UserControl
{
    public partial class ucAssignment : DevExpress.XtraEditors.XtraUserControl
    {
        public ucAssignment()
        {
            InitializeComponent();
            //rdoStatus.SelectedIndex = 4;
            dDatetime1.DateTime = DateTime.Now;
            dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
        }

        //public ucAssignment(List<string> CrewsId)
        //{
        //    InitializeComponent();

        //}

        public List<string> crewList;
        AssignmentDataContext data = new AssignmentDataContext();

        private void rdoStatus_Properties_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dsVessel.tblVessel.Count == 0)
            {
                this.tblVesselTableAdapter.Fill(dsVessel.tblVessel);
            }
            switch (rdoStatus.SelectedIndex)
            {
                case 0:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    noiLenLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    noiXuongLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = false;
                    layoutDatetime1.Text = ((BaseForm)ParentForm).rm.GetString("sDateOnBoard", ((BaseForm)ParentForm).myCultureInfo);//Thời gian lên tàu
                    layoutDatetime2.Text = ((BaseForm)ParentForm).rm.GetString("sDateContract", ((BaseForm)ParentForm).myCultureInfo);//Thời gian hợp đồng
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateLeft", ((BaseForm)ParentForm).myCultureInfo);//Thời gian dự kiến rời tàu
                    tblVesselBindingSource.Position = 0;
                    QLTV.Database.dsVessel.tblVesselRow row = dsVessel.tblVessel[0];
                    lkVessel.EditValue = row.nVesselId;
                    break;
                case 1:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiLenLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiXuongLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    dDatetime3.Enabled = false;
                    layoutDatetime1.Text = ((BaseForm)ParentForm).rm.GetString("sDateLeftOnBoard", ((BaseForm)ParentForm).myCultureInfo);//Thời gian rời tàu
                    layoutDatetime2.Text = ((BaseForm)ParentForm).rm.GetString("sDateRequired", ((BaseForm)ParentForm).myCultureInfo);//Thời gian yêu cầu nghỉ
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateReady", ((BaseForm)ParentForm).myCultureInfo);//Thời gian dự kiến sẵn sàng lên tàu
                    break;
                case 2:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiLenLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiXuongLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateExprieContract", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm cắt hợp đồng
                    break;
                case 3:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiLenLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiXuongLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateRetired", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm nghỉ hưu
                    break;
                case 4:
                    layoutDatetime1.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutDatetime2.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    layoutVessel.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiLenLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    noiXuongLayoutControl.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
                    dDatetime3.Enabled = true;
                    layoutDatetime3.Text = ((BaseForm)ParentForm).rm.GetString("sDateRegisterContract", ((BaseForm)ParentForm).myCultureInfo);//Thời điểm ký hợp đồng tuyển dụng
                    break;
                default:
                    break;
            }
        }

        private void ucAssignment_Load(object sender, EventArgs e)
        {
            if (crewList != null && crewList.Count != 0)
            {
                tblAssignment a = data.tblAssignments.SingleOrDefault(u => u.nCrewid == crewList.FirstOrDefault() && u.isActive == true);
                if (a != null)
                {
                    switch (a.cStatus.ToUpper())
                    {
                        case "ON BOARD":
                            rdoStatus.SelectedIndex = 0;
                            dDatetime1.EditValue = a.dEmbarked;
                            dDatetime2.EditValue = a.nContract;
                            dDatetime3.EditValue = a.dDuKienRoiTau;
                            lkVessel.EditValue = a.nVesselId;
                            txtNoiĐi.EditValue = a.cNoiDi;
                            txtNoiXuong.EditValue = a.cNoiXuong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON VACATION":
                            rdoStatus.SelectedIndex = 1;
                            dDatetime1.EditValue = a.dLeave;
                            dDatetime2.EditValue = a.nRequest;
                            dDatetime3.EditValue = a.dSanSangLenTau;
                            txtNoiĐi.EditValue = a.cNoiDi;
                            txtNoiXuong.EditValue = a.cNoiXuong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON RESIGN":
                            rdoStatus.SelectedIndex = 2;
                            dDatetime3.EditValue = a.dCatHopDong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "ON RETIRE":
                            rdoStatus.SelectedIndex = 3;
                            dDatetime3.EditValue = a.dNghiHuu;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        case "NEW CREW":
                            rdoStatus.SelectedIndex = 4;
                            dDatetime3.EditValue = a.dKyHopDong;
                            cGhiChuMemoEdit.EditValue = a.cGhiChu;
                            break;
                        default:
                            break;
                    }
                }

            }

            this.tblVesselTableAdapter.Fill(dsVessel.tblVessel);
        }

        private void dDatetime1_Properties_DateTimeChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(dDatetime2.Value.ToString()))
            {
                dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
            }
        }

        private void dDatetime2_Properties_ValueChanged(object sender, EventArgs e)
        {
            if (dDatetime1.DateTime != null)
            {
                dDatetime3.DateTime = dDatetime1.DateTime.AddMonths(int.Parse(dDatetime2.Value.ToString()));
            }
        }

        public bool btnSaveVisible
        {
            get
            {
                return layoutControlItem6.IsHidden;
            }
            set
            {
                if (value)
                {
                    layoutControlItem6.ShowInCustomizationForm = true;
                }
                else
                {
                    layoutControlItem6.HideToCustomization();
                }
            }
        }

        public bool btnCancelVisible
        {
            get
            {
                return layoutControlItem7.IsHidden;
            }
            set
            {
                if (value)
                {
                    layoutControlItem7.ShowInCustomizationForm = true;
                }
                else
                {
                    layoutControlItem7.HideToCustomization();
                }

            }
        }

        #region Save Handler
        public delegate void SaveHandler(object sender, EventArgs e);
        public event SaveHandler SaveClick;





        void onSaveClick(object sender, EventArgs e)
        {
            if (SaveClick != null)
            {
                SaveClick(sender, e);
            }
        }



        public void saveAssignment(object sender, EventArgs e)
        {
            try
            {
                List<tblAssignment> Assignments = new List<tblAssignment>();
                foreach (string CrewId in this.crewList)
                {
                    var query = from a in data.tblAssignments
                                where a.nCrewid == CrewId
                                select a;
                    if (query != null && query.Count() == 5) //chinh sua
                    {
                        foreach (tblAssignment a in query)
                        {
                            a.isActive = false;
                            Assignments.Add(a);
                        }
                    }
                    else// them moi
                    {
                        if (query != null)
                        {
                            data.tblAssignments.DeleteAllOnSubmit(query);
                        }
                        tblAssignment a1 = new tblAssignment();
                        tblAssignment a2 = new tblAssignment();
                        tblAssignment a3 = new tblAssignment();
                        tblAssignment a4 = new tblAssignment();
                        tblAssignment a5 = new tblAssignment();

                        a1.cStatus = "On Board";
                        a1.nCrewid = CrewId;
                        a1.isActive = false;

                        a2.cStatus = "On Vacation";
                        a2.nCrewid = CrewId;
                        a2.isActive = false;

                        a3.cStatus = "On Resign";
                        a3.nCrewid = CrewId;
                        a3.isActive = false;

                        a4.cStatus = "On Retire";
                        a4.nCrewid = CrewId;
                        a4.isActive = false;

                        a5.cStatus = "New Crew";
                        a5.nCrewid = CrewId;
                        a5.isActive = false;

                        Assignments.Add(a1);
                        Assignments.Add(a2);
                        Assignments.Add(a3);
                        Assignments.Add(a4);
                        Assignments.Add(a5);
                        data.tblAssignments.InsertAllOnSubmit(Assignments);
                    }
                }


                switch (rdoStatus.SelectedIndex)
                {
                    case 0:
                        foreach (tblAssignment assignment in Assignments)
                        {
                            if (assignment.cStatus.ToUpper() == "ON BOARD")
                            {
                                //experience
                                tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                                //truong hop giữ nguyên trạng thái ON BOARD
                                if (current1 != null && current1.cStatus.ToUpper() == "ON BOARD")
                                {
                                    if (current1.ExpID != null)
                                    {

                                        data.tblExpriences.SingleOrDefault(u => u.nExprienceId == current1.ExpID).dEmbarkedDate = dDatetime1.DateTime;
                                        data.tblExpriences.SingleOrDefault(u => u.nExprienceId == current1.ExpID).nCrewId = current1.nCrewid;
                                        data.tblExpriences.SingleOrDefault(u => u.nExprienceId == current1.ExpID).nEmbarkedPlace = txtNoiĐi.Text;
                                        if (lkVessel.EditValue != null)
                                            data.tblExpriences.SingleOrDefault(u => u.nExprienceId == current1.ExpID).nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                        data.tblExpriences.SingleOrDefault(u => u.nExprienceId == current1.ExpID).nRankId = current1.tblCrew.nRankID;

                                    }

                                }
                                else//nếu từ trạng thái khác chuyển sang ON BOARD
                                {
                                    tblExprience exp = new tblExprience();
                                    if (current1.nCrewid != null)
                                        exp.nCrewId = current1.nCrewid;
                                    if (dDatetime1.DateTime != null)
                                        exp.dEmbarkedDate = dDatetime1.DateTime;
                                    exp.nEmbarkedPlace = txtNoiĐi.Text;
                                    if (lkVessel.EditValue != null)
                                        exp.nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                    exp.nRankId = current1.tblCrew.nRankID;
                                    data.tblExpriences.InsertOnSubmit(exp);
                                    data.SubmitChanges();
                                    //lấy ExperienID để cập nhật kinh nghiệm
                                    assignment.ExpID = exp.nExprienceId;
                                }
                                //ghi nhật ký
                                if (current1.cStatus.ToUpper() == "ON BOARD")
                                {
                                    if (current1.LogID != null)
                                    {
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cStatus = assignment.cStatus;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nCrewid = assignment.nCrewid;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dModified = DateTime.Now;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nContract = int.Parse(dDatetime2.Value.ToString());
                                        if (txtNoiĐi.EditValue != null)
                                            data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cNoiDi = txtNoiĐi.EditValue.ToString();
                                        if (cGhiChuMemoEdit.EditValue != null)
                                            data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dDuKienRoiTau = dDatetime3.DateTime;
                                    }
                                }
                                else
                                {
                                    tblAssignmentLog log = new tblAssignmentLog();
                                    log.cStatus = assignment.cStatus;
                                    log.nCrewid = assignment.nCrewid;
                                    log.dModified = DateTime.Now;
                                    log.dEmbarked = dDatetime1.DateTime;
                                    log.nContract = int.Parse(dDatetime2.Value.ToString());
                                    if (txtNoiĐi.EditValue != null)
                                        log.cNoiDi = txtNoiĐi.EditValue.ToString();
                                    log.nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                    if (cGhiChuMemoEdit.EditValue != null)
                                    {
                                        log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                    }
                                    log.dDuKienRoiTau = dDatetime3.DateTime;
                                    data.tblAssignmentLogs.InsertOnSubmit(log);
                                    data.SubmitChanges();
                                    assignment.LogID = log.nAssignmentId;
                                }
                                //ghi assignment
                                assignment.isActive = true;
                                assignment.dEmbarked = dDatetime1.DateTime;
                                assignment.nContract = int.Parse(dDatetime2.Value.ToString());
                                assignment.nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                if (txtNoiĐi.EditValue != null)
                                    assignment.cNoiDi = txtNoiĐi.EditValue.ToString();
                                assignment.dDuKienRoiTau = dDatetime3.DateTime;
                                if (cGhiChuMemoEdit.EditValue != null)
                                {
                                    assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                }

                            }
                        }
                        break;
                    case 1:
                        foreach (tblAssignment assignment in Assignments)
                        {
                            if (assignment.cStatus.ToUpper() == "ON VACATION")
                            {
                                //tblAssignment onboard = data.tblAssignments.SingleOrDefault(u => u.nCrewid == assignment.nCrewid && u.cStatus.ToUpper() == "ON BOARD" && u.isActive == true);
                                tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                                //truong hợp giữ nguyên trạng thái
                                if (current1 != null)
                                {

                                    // data.tblExpriences.get
                                    //  exp.nCrewId = current1.nCrewid;
                                    //exp.dEmbarkedDate = current1.dEmbarked;
                                    if (data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID) != null)
                                    {
                                        data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).dDisembarkedDate = dDatetime1.DateTime;
                                        // exp.nEmbarkedPlace = current1.cNoiDi;
                                        data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).nDisembarkedPlace = txtNoiXuong.EditValue.ToString();
                                        // exp.nVesselId = current1.nVesselId;
                                        //  exp.nRankId = e.rankID;
                                        //Math.Round((double)d.Days / 30, 1);
                                        data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).fDuration = Math.Round((double)(dDatetime1.DateTime.Subtract((DateTime)data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).dEmbarkedDate).Days / 30), 1);
                                    }
                                    //data.tblExpriences.u(exp);

                                }



                                //trương hợp ko thay đổi trạng thái On vacation
                                if (current1.cStatus.ToUpper() == "ON VACATION")
                                {
                                    if (current1.LogID != null)
                                    {
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cStatus = assignment.cStatus;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nCrewid = assignment.nCrewid;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dModified = DateTime.Now;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nVesselId = int.Parse(lkVessel.EditValue.ToString());
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nContract = int.Parse(dDatetime2.Value.ToString());
                                        if (txtNoiĐi.EditValue != null)
                                            data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cNoiDi = txtNoiĐi.EditValue.ToString();
                                        if (cGhiChuMemoEdit.EditValue != null)
                                            data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dDuKienRoiTau = dDatetime3.DateTime;
                                    }
                                }
                                else
                                {
                                    tblAssignmentLog log = new tblAssignmentLog();

                                    log.cStatus = assignment.cStatus;
                                    log.nCrewid = assignment.nCrewid;
                                    log.dModified = DateTime.Now;
                                    log.dLeave = dDatetime1.DateTime;
                                    if (txtNoiXuong.EditValue != null)
                                        log.cNoiXuong = txtNoiXuong.EditValue.ToString();
                                    log.nRequest = int.Parse(dDatetime2.Value.ToString());
                                    if (cGhiChuMemoEdit.EditValue != null)
                                    {
                                        log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                    }

                                    log.dSanSangLenTau = dDatetime3.DateTime;
                                    data.tblAssignmentLogs.InsertOnSubmit(log);
                                    data.SubmitChanges();
                                    assignment.LogID = log.nAssignmentId;
                                }
                                assignment.isActive = true;
                                assignment.dLeave = dDatetime1.DateTime;
                                assignment.nRequest = int.Parse(dDatetime2.Value.ToString());
                                if (txtNoiXuong.EditValue != null)
                                    assignment.cNoiXuong = txtNoiXuong.EditValue.ToString();
                                assignment.dSanSangLenTau = dDatetime3.DateTime;
                                if (cGhiChuMemoEdit.EditValue != null)
                                    assignment.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                            }
                            //else
                            //{
                            //    tblAssignment current = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                            //    if (current != null && assignment.cStatus == current.cStatus)
                            //    {
                            //        assignment.cDescription = current.cDescription;
                            //        assignment.cStatus = current.cStatus;
                            //        assignment.dCatHopDong = current.dCatHopDong;
                            //        assignment.dDuKienRoiTau = current.dDuKienRoiTau;
                            //        assignment.dEmbarked = current.dEmbarked;
                            //        assignment.dKyHopDong = current.dKyHopDong;
                            //        assignment.dLeave = current.dLeave;
                            //        assignment.dModified = current.dModified;
                            //        assignment.dNghiHuu = current.dNghiHuu;
                            //        assignment.dSanSangLenTau = current.dSanSangLenTau;
                            //        assignment.isActive = true;
                            //        assignment.nContract = current.nContract;
                            //        assignment.nCrewid = current.nCrewid;
                            //        assignment.nRequest = current.nRequest;
                            //        assignment.nVesselId = current.nVesselId;
                            //        assignment.cGhiChu = current.cGhiChu;
                            //    }
                            //}
                        }
                        break;
                    case 2:
                        foreach (tblAssignment assignment in Assignments)
                        {
                            if (assignment.cStatus.ToUpper() == "ON RESIGN")
                            {
                                tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                                if (current1.cStatus.ToUpper() == "ON RESIGN")
                                {
                                    if (current1.LogID != null)
                                    {
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cStatus = assignment.cStatus;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dCatHopDong = dDatetime3.DateTime;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nCrewid = assignment.nCrewid;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dModified = DateTime.Now;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cGhiChu = cGhiChuMemoEdit.Text;

                                    }
                                }
                                else
                                {
                                    tblAssignmentLog log = new tblAssignmentLog();
                                    log.cStatus = assignment.cStatus;
                                    log.dCatHopDong = dDatetime3.DateTime;
                                    log.nCrewid = assignment.nCrewid;
                                    log.dModified = DateTime.Now;
                                    log.cGhiChu = cGhiChuMemoEdit.EditValue.ToString();
                                    data.tblAssignmentLogs.InsertOnSubmit(log);
                                    data.SubmitChanges();
                                    assignment.LogID = log.nAssignmentId;
                                }
                                assignment.isActive = true;
                                assignment.dCatHopDong = dDatetime3.DateTime;
                                assignment.cGhiChu = cGhiChuMemoEdit.Text;
                            }
                        }
                        break;
                    case 3:
                        foreach (tblAssignment assignment in Assignments)
                        {
                            if (assignment.cStatus.ToUpper() == "ON RETIRE")
                            {
                                  tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                                  if (current1.cStatus.ToUpper() == "ON RESIGN")
                                  {
                                      if (current1.LogID != null)
                                      {
                                          data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cStatus = assignment.cStatus;
                                          data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dNghiHuu = dDatetime3.DateTime;
                                          data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nCrewid = assignment.nCrewid;
                                          data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dModified = DateTime.Now;
                                          data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cGhiChu = cGhiChuMemoEdit.Text;

                                      }
                                  }
                                  else
                                  {
                                      tblAssignmentLog log = new tblAssignmentLog();
                                      log.cStatus = assignment.cStatus;
                                      log.dNghiHuu = dDatetime3.DateTime;
                                      log.nCrewid = assignment.nCrewid;
                                      log.dModified = DateTime.Now;
                                      log.cGhiChu = cGhiChuMemoEdit.Text;
                                      data.tblAssignmentLogs.InsertOnSubmit(log);
                                      data.SubmitChanges();
                                      assignment.LogID = log.nAssignmentId;
                                  }
                                assignment.isActive = true;
                                assignment.dNghiHuu = dDatetime3.DateTime;
                                assignment.cGhiChu = cGhiChuMemoEdit.Text;
                            }
                        }
                        break;
                    case 4:
                        foreach (tblAssignment assignment in Assignments)
                        {
                            if (assignment.cStatus.ToUpper() == "NEW CREW")
                            {
                                tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                                if (current1.cStatus.ToUpper() == "ON RESIGN")
                                {
                                    if (current1.LogID != null)
                                    {
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cStatus = assignment.cStatus;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dKyHopDong = dDatetime3.DateTime;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).nCrewid = assignment.nCrewid;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).dModified = DateTime.Now;
                                        data.tblAssignmentLogs.SingleOrDefault(u => u.nAssignmentId == current1.LogID).cGhiChu = cGhiChuMemoEdit.Text;

                                    }
                                }
                                else
                                {
                                    tblAssignmentLog log = new tblAssignmentLog();
                                    log.cStatus = assignment.cStatus;
                                    log.dNghiHuu = dDatetime3.DateTime;
                                    log.nCrewid = assignment.nCrewid;
                                    log.dModified = DateTime.Now;
                                    log.cGhiChu = cGhiChuMemoEdit.Text;
                                    data.tblAssignmentLogs.InsertOnSubmit(log);
                                    data.SubmitChanges();
                                    assignment.LogID = log.nAssignmentId;
                                }


                                assignment.isActive = true;
                                assignment.dKyHopDong = dDatetime3.DateTime;
                                assignment.cGhiChu = cGhiChuMemoEdit.Text;
                            }
                        }
                        break;
                    default:
                        break;
                }
                data.SubmitChanges();
                onSaveClick(sender, e);
            }
            catch
            { }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveAssignment(sender, e);

        }

        public delegate void CancelHandler(object sender, EventArgs e);

        public event CancelHandler CancelClick;

        void onCancelClick(object sender, EventArgs e)
        {
            if (CancelClick != null)
            {
                CancelClick(sender, e);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            onCancelClick(sender, e);
        }
        #endregion




        #region Short plan



        public int updateOnBoard(MyArgs e)
        {
            //try
            //{
            //set isactive =false 
            List<tblAssignment> Assignments = new List<tblAssignment>();
            //foreach (string CrewId in this.crewList)
            //{
            var query = from a in data.tblAssignments
                        where a.nCrewid == e.crewIDNextLine
                        select a;
            if (query != null && query.Count() == 5)
            {
                foreach (tblAssignment a in query)
                {
                    a.isActive = false;
                    Assignments.Add(a);
                }
            }
            //}
            //len tau
            foreach (tblAssignment assignment in Assignments)
            {
                if (assignment.cStatus.ToUpper() == "ON BOARD")
                {

                    //experience
                    tblAssignment onboard = data.tblAssignments.SingleOrDefault(u => u.nCrewid == assignment.nCrewid && u.cStatus.ToUpper() == "ON BOARD" && u.isActive == true);
                    tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);
                    tblExprience exp = new tblExprience();

                    //if (current1 != null)
                    //{
                    //current1.isActive = false;

                    //if (onboard != null)
                    //{

                    exp.nCrewId = e.crewIDNextLine;
                    exp.dEmbarkedDate = e.ngayLenTau;
                    //  exp.dDisembarkedDate = e.ngayxuong;
                    exp.nEmbarkedPlace = e.NoiLen;
                    //exp.nDisembarkedPlace = e.NoiRoi;
                    exp.nVesselId = e.vesselID;
                    exp.nRankId = e.rankID;
                    //Math.Round((double)d.Days / 30, 1);
                    // exp.fDuration = Math.Round((double)((DateTime)e.ngayxuong).Subtract((DateTime)current1.dEmbarked).Days / 30, 1);
                    data.tblExpriences.InsertOnSubmit(exp);
                    data.SubmitChanges();

                    //}
                    // }
                    //assignment
                    assignment.isActive = true;
                    assignment.ExpID = exp.nExprienceId;
                    assignment.dEmbarked = e.ngayLenTau;
                    assignment.nContract = e.hopdong;
                    assignment.nVesselId = e.vesselID;
                    assignment.cNoiDi = e.NoiLen;
                    assignment.cGhiChu = "";
                    assignment.dDuKienRoiTau = e.ngayLenTau.AddMonths(e.hopdong);
                    //insert to log 
                    tblAssignmentLog log = new tblAssignmentLog();
                    log.cStatus = assignment.cStatus;
                    log.nCrewid = assignment.nCrewid;
                    log.dModified = DateTime.Now;
                    log.nVesselId = assignment.nVesselId;
                    log.dEmbarked = e.ngayLenTau;
                    log.nContract = e.hopdong;
                    log.nVesselId = e.vesselID;
                    log.cGhiChu = "";
                    log.cNoiDi = e.NoiLen;
                    log.dDuKienRoiTau = e.ngayLenTau.AddMonths(e.hopdong);

                    data.tblAssignmentLogs.InsertOnSubmit(log);



                }
            }
            data.SubmitChanges();
            return 1;
            //}
            //catch (Exception ex)
            //{
            //    return -1;
            //}

        }

        public int updateOnVacation(MyArgs e)
        {
            //try
            //{
            //set isactive =false 
            List<tblAssignment> Assignments = new List<tblAssignment>();

            var query = from a in data.tblAssignments
                        where a.nCrewid == e.crewID
                        select a;

            if (query != null && query.Count() == 5)
            {
                foreach (tblAssignment a in query)
                {
                    a.isActive = false;
                    Assignments.Add(a);
                }
            }

            foreach (tblAssignment assignment in Assignments)
            {
                if (assignment.cStatus.ToUpper() == "ON VACATION")
                {
                    tblAssignment onboard = data.tblAssignments.SingleOrDefault(u => u.nCrewid == assignment.nCrewid && u.cStatus.ToUpper() == "ON BOARD" && u.isActive == true);
                    tblAssignment current1 = data.tblAssignments.SingleOrDefault(u => u.isActive == true && u.nCrewid == assignment.nCrewid);

                    if (current1 != null)
                    {
                        current1.isActive = false;

                        if (onboard != null)
                        {
                            // data.tblExpriences.get
                            //  exp.nCrewId = current1.nCrewid;
                            //exp.dEmbarkedDate = current1.dEmbarked;
                            if (data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID) != null)
                            {
                                data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).dDisembarkedDate = e.ngayxuong;
                                // exp.nEmbarkedPlace = current1.cNoiDi;
                                data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).nDisembarkedPlace = e.NoiRoi;
                                // exp.nVesselId = current1.nVesselId;
                                //  exp.nRankId = e.rankID;
                                //Math.Round((double)d.Days / 30, 1);
                                data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).fDuration = Math.Round((double)((DateTime)e.ngayxuong).Subtract((DateTime)data.tblExpriences.SingleOrDefault(exp => exp.nExprienceId == current1.ExpID).dEmbarkedDate).Days / 30, 1);
                            }
                            //data.tblExpriences.u(exp);
                        }
                    }

                    tblAssignmentLog log = new tblAssignmentLog();

                    assignment.isActive = true;
                    assignment.dLeave = e.ngayxuong;
                    assignment.nRequest = 3;
                    assignment.cGhiChu = "";
                    assignment.dSanSangLenTau = e.ngayxuong.AddMonths(3);
                    assignment.cNoiXuong = e.NoiRoi;

                    log.cStatus = assignment.cStatus;
                    log.nCrewid = assignment.nCrewid;
                    log.dModified = DateTime.Now;
                    log.dLeave = e.ngayxuong;
                    log.nRequest = 3;
                    log.cNoiXuong = e.NoiRoi;
                    log.cGhiChu = "";

                    log.dSanSangLenTau = e.ngayxuong.AddMonths(3);
                    data.tblAssignmentLogs.InsertOnSubmit(log);
                }
            }
            data.SubmitChanges();
            return 1;
            //}
            //catch (Exception ex)
            //{
            //    return -1;
            //}
        }
        #endregion



    }
}
